package jmine.tec.services.environment;

import java.io.InputStream;
import java.net.URL;
import java.util.Arrays;

import jmine.tec.environment.db.DBEnvironment;

import org.apache.commons.io.IOUtils;

import bancosys.tec.utils.db.descriptor.ReferenceDatabaseEntry;

/**
 * Entry loader that delegates to the DataDigesterController
 * 
 * @author lundberg
 */
public class DigesterPostscriptEntryLoader extends AbstractRefdbPostscriptEntryLoader {

    /**
     * {@inheritDoc}
     */
    @Override
    protected boolean matches(ReferenceDatabaseEntry entry) {
        return !Arrays.asList("sql", "service", "schema").contains(entry.getType().toLowerCase());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void doLoad(DBEnvironment environment, ReferenceDatabaseEntry entry, URL url) throws Exception {
        if (!this.getDataDigesterController(environment).getFileCodes().contains(entry.getType())) {
            this.getDataDigesterController(environment).clearDriverRegistry();
        }
        InputStream stream = url.openStream();
        try {
            this.getDataDigesterController(environment).fastProcess(entry.getType(), entry.getClasspathResourcePath(), stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }
}
